# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=ufraw
pkgbase=mingw-w64-gimp-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-gimp-${_realname}"
groups=("${MINGW_PACKAGE_PREFIX}-gimp-plugins")
pkgver=0.22
pkgrel=2
pkgdesc="Converter for raw files; utility and GIMP plugin (mingw-w64)"
arch=('any')
url="https://ufraw.sourceforge.io/"
license=("GPL")
options=('strip' 'staticlibs')
depends=("${MINGW_PACKAGE_PREFIX}-bzip2"
         "${MINGW_PACKAGE_PREFIX}-cfitsio"
         "${MINGW_PACKAGE_PREFIX}-exiv2"
         "${MINGW_PACKAGE_PREFIX}-gtkimageview"
         "${MINGW_PACKAGE_PREFIX}-lcms"
         "${MINGW_PACKAGE_PREFIX}-lensfun")
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc"
             "${MINGW_PACKAGE_PREFIX}-gimp")
optdepends=("${MINGW_PACKAGE_PREFIX}-gimp: to use the gimp import plugin for raw images")
source=("https://downloads.sourceforge.net/${_realname}/${_realname}-${pkgver}.tar.gz"
        01-mingw-getopt-long.patch
        02-mingw-w64-fixes.patch
        03-fix-abs-gcc-7.patch
        04-gcc-9-openmp.patch
        05-exiv2-0.27.patch)
sha256sums=('f7abd28ce587db2a74b4c54149bd8a2523a7ddc09bedf4f923246ff0ae09a25e'
            '41c7c530b6c8992653166ba2adb155c3ae137eb12ed7bc8675d623fd58a6bb13'
            '9bbb7748bdb3bb9e085600b827a061d91e938852aae80e51da082f030901cd25'
            '1cac31243d5a4e647a8263785b653cf33efb6ebccb660a22a8170a22fe0a09ff'
            '1edce8bea1bd3bbf46a0d8f3eb172fdf3c646e215f829bd8a6e1fdc560646c28'
            'fd9f9dab2c96c0da5c4c0be83d53672a46f3fe4d3f84b4dc635037f971c6743e')

prepare() {
  cd "${srcdir}/${_realname}-${pkgver}"
  patch -p1 -i ${srcdir}/01-mingw-getopt-long.patch
  patch -p1 -i ${srcdir}/02-mingw-w64-fixes.patch
  patch -p1 -i ${srcdir}/03-fix-abs-gcc-7.patch
  patch -p1 -i ${srcdir}/04-gcc-9-openmp.patch
  patch -p1 -i ${srcdir}/05-exiv2-0.27.patch
  ./autogen.sh
}

build() {
  [[ -d build-${MINGW_CHOST} ]] && rm -rf build-${MINGW_CHOST}
  mkdir -p build-${MINGW_CHOST} && cd build-${MINGW_CHOST}
  MSYS2_ARG_CONV_EXCL="GIMP_LIBDIR" \
  ../${_realname}-${pkgver}/configure \
    --prefix=${MINGW_PREFIX} \
    --build=${MINGW_CHOST} \
    --host=${MINGW_CHOST} \
    --enable-extras \
    --enable-mime \
    --with-gimp \
    --enable-openmp \
    --enable-contrast

  local wgimp_pref=$(cygpath -wm ${MINGW_PREFIX})
  sed -e "s|${wgimp_pref}|${MINGW_PREFIX}|g" -i Makefile
  sed -e "s|-ffast-math -fomit-frame-pointer -W -Wall -O3|${CFLAGS}|" -i Makefile
  make VERBOSE=1
}

package() {
  cd "${srcdir}/build-${MINGW_CHOST}"
  make -j1 DESTDIR="${pkgdir}" install
}
